<?php

namespace App\Services\Common;

use App\Helpers\JwtUtils;
use App\Models\Account\UserAccountModel;
use App\Models\Account\UserAccountSkuModel;
use App\Models\Assess\OrderAndCustomersOrderDetailModel;
use App\Models\Assess\OrderAndCustomersOrderModel;
use App\Models\Assess\SendLogModel;
use App\Models\Assess\VisitorsRecordModel;
use App\Models\Register\ActivateUserManagementInfoModel;
use App\Models\Register\ActivateUserManagementModel;
use App\Models\UserModel;
use App\Services\Assess\OrderAndCustomersOrderService;
use App\Services\Assess\SendLogService;
use App\Services\Register\RegisterService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class RedisService
{


    /**
     * @Notes:获取【用户与账号管理】 对应账号信息
     * @Interface getAccountList
     * @author: wuyu
     * @Time: 2023/6/6   12:04
     */
    public static function getAccountList()
    {
        $OrderAndCustomersOrderModel = new OrderAndCustomersOrderModel();
        $userId = $OrderAndCustomersOrderModel->userId;
        $redisKey = 'account_list:'.$userId;
        $data = Redis::get($redisKey);
        if (empty($data)){
            if (!in_array($userId,$OrderAndCustomersOrderModel->userAllIds)){
                $UserAccountModel = new UserAccountModel();
                $userGroupIdOne = $UserAccountModel->getOne([
                    ['user_id','=',$userId]
                ]);
                if (!empty($userGroupIdOne)){
                    $data = $UserAccountModel
                        ->where('user_id','=',$userGroupIdOne['user_id'])
                        ->where('mark','=',1)
                        ->groupBy(['account_abbr','account_name'])
                        ->get(['account_abbr as platform_user_name','account_name as user_account'])
                        ->toArray();
                }else{
                    $data = $UserAccountModel
                        ->where('user_group_id','=',$userId)
                        ->where('mark','=',1)
                        ->groupBy(['account_abbr','account_name'])
                        ->get(['account_abbr as platform_user_name','account_name as user_account'])
                        ->toArray();
                }
            }else{
                $data = $OrderAndCustomersOrderModel->groupBy(['user_account','platform_user_name'])->get(['user_account','platform_user_name'])->toArray();
            }

            Redis::setex($redisKey, 86400, json_encode($data));
            return $data;
        }
        return json_decode($data,true);

    }

    /**
     * @Notes:获取分类信息
     * @Interface getCategoryList
     * @return mixed
     * @author: wuyu
     * @Time: 2023/6/6   12:19
     */
    public static function getCategoryList()
    {
        $redisKey = 'category_list';
        $data = Redis::get($redisKey);
        if (empty($data)){
            $OrderAndCustomersOrderDetailModel = new OrderAndCustomersOrderDetailModel();
            $data = $OrderAndCustomersOrderDetailModel->where('product_category_name1','!=','')->groupBy(['product_category_name1'])->get(['product_category_name1'])->toArray();
            Redis::setex($redisKey, 86400, json_encode($data));
            return $data;
        }
        return json_decode($data,true);
    }


    /**
     * @Notes:获取用户信息
     * @Interface getUserList
     * @author: wuyu
     * @Time: 2023/6/26   16:39
     */
    public static function getUserAccountList()
    {
        $redisKey = 'user_account_list';
        $data = Redis::get($redisKey);
        if (empty($data)){
            $UserModel = new UserModel();
            $data = $UserModel->where('status','=',1)->where('mark','=',1)->get(['id','realname'])->toArray();
            Redis::setex($redisKey, 20*60, json_encode($data));
            return $data;
        }
        return json_decode($data,true);
    }

    /**
     * @Notes:获取【用户与账号管理】 对应用户信息
     * @Interface getUserList
     * @author: wuyu
     * @Time: 2023/6/26   16:39
     */
    public static function getUserList()
    {
        $UserModel = new UserModel();
        $redisKey = 'user_list:'.$UserModel->userId;
        $data = Redis::get($redisKey);
        if (empty($data)){
            if (!in_array($UserModel->userId,$UserModel->userAllIds)){
                $UserAccountModel = new UserAccountModel();
                $userGroupIdOne = $UserAccountModel->getOne([
                    ['user_group_id','=',$UserModel->userId]
                ]);
                if (!empty($userGroupIdOne)){
                    $userGroupList = $UserAccountModel->where('user_id','=',$userGroupIdOne['user_id'])->where('mark','=',1)->get(['user_id','user_group_id'])->toArray();
                    if (!empty($userGroupList)){
                        $userList = array_unique(array_column($userGroupList,'user_group_id'));
                        $data = $UserModel->where('status','=',1)->where('mark','=',1)->whereIn('id',$userList)->get(['id','realname'])->toArray();
                    }
                }
            }else{
                $data = $UserModel->where('status','=',1)->where('mark','=',1)->get(['id','realname'])->toArray();
            }
            Redis::setex($redisKey, 20*60, json_encode($data));
            return $data;
        }
        return json_decode($data,true);
    }

    /**
     * @Notes:获取数据看板缓存信息
     * @Interface getDataBoardRedis
     * @return array|mixed|string
     * @author: wuyu
     * @Time: 2023/6/12   10:36
     */
    public static function getDataBoardRedis()
    {
        $userId = JwtUtils::getUserId();
        $redisKey = 'data_board_list:'.$userId;
        $data = Redis::get($redisKey);
        if (empty($data)){
            $data = self::getDataBoard();
            Redis::setex($redisKey, 20*60, json_encode($data)); //缓存数据20分钟
            return $data;
        }
        return json_decode($data,true);
    }

    /**
     * @Notes:获取asin对应的国家信息
     * @Interface getAsinAndCountry
     * @author: wuyu
     * @Time: 2023/6/12   10:39
     */
    public static function getAsinAndCountry()
    {
        $redisKey = 'asin_country_list';
        $data = Redis::get($redisKey);
        if (empty($data)){
            $OrderAndCustomersOrderModel = new OrderAndCustomersOrderModel();
            $OrderAndCustomersOrderDetailModel = new OrderAndCustomersOrderDetailModel();
            $OrderAndCustomersOrderService = new OrderAndCustomersOrderService();
            $mainTable = $OrderAndCustomersOrderModel->getTable();
            $sonTable = $OrderAndCustomersOrderDetailModel->getTable();
            $data = $OrderAndCustomersOrderModel::from("$mainTable as m")
                ->select(['s.op_site', 's.asin'])
                ->join("$sonTable as s", 's.order_id', '=', 'm.id')
                ->where('m.platform','=','amazon')
                ->where('s.op_site','!=','')
                ->groupBy(['s.op_site','s.asin'])
                ->get()->toArray();
            foreach ($data as $key=>$val){
                $data[$key]['asin_country_name'] =($OrderAndCustomersOrderService->country[$val['op_site']] ?? '') .'('.$val['op_site'].')'.'-'.$val['asin'];
            }
            Redis::setex($redisKey, 3*60*60, json_encode($data)); //缓存数据半天
            return $data;
        }
        return json_decode($data,true);

    }


    /**
     * @Notes:获取数据看板相关信息
     * @Interface getDataBoard
     * @author: wuyu
     * @Time: 2023/6/8   10:50
     */
    public static function getDataBoard()
    {
        $data = [];
        $nowDate = date('Y-m-d',time());
        $startDate = $nowDate.' 00:00:00';
        $endData = $nowDate.' 23:59:59';
        //获取访客数
        $VisitorsRecordModel = new VisitorsRecordModel();
        $ActivateUserManagementModel = new ActivateUserManagementModel();
        $ActivateUserManagementInfoModel = new ActivateUserManagementInfoModel();
        $OrderAndCustomersOrderModel = new OrderAndCustomersOrderModel();
        $OrderAndCustomersOrderDetailModel = new OrderAndCustomersOrderDetailModel();
        $SendLogModel = new SendLogModel();

        //权限过滤 组员可以看一个组的信息
        $createUserList = CommonService::userAuth();
        $skuAuth = CommonService::skuAuth();

        //访客数 不需要权限过滤
        $data['visitor_count_now'] = $VisitorsRecordModel
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData))
            ->count();
        $data['visitor_count_all'] = $VisitorsRecordModel->count();

        //激活成功客户数 今日
        $activateModel = $ActivateUserManagementModel
            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData));

        if (!empty($skuAuth)) $activateModel->whereIn('platform_sku',$skuAuth);
        $data['activate_success_count_now']=$activateModel->count();

        //激活成功客户数 总数
        $activateAllModel = $ActivateUserManagementModel
            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('mark','=',1);
        if (!empty($skuAuth)) $activateAllModel->whereIn('platform_sku',$skuAuth);
           $data['activate_success_count_all'] = $activateAllModel->count();

        //获取激活异常数 今日数
        $activateExceptionModel = $ActivateUserManagementModel
            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ABNORMAL)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $activateExceptionModel->whereIn('platform_sku',$skuAuth);
            $data['activate_exception_count_now']=$activateExceptionModel->count();

        //获取激活异常数 总数
        $activateExceptionAllModel = $ActivateUserManagementModel
            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ABNORMAL)
            ->where('mark','=',1);
        if (!empty($skuAuth)) $activateExceptionAllModel->whereIn('platform_sku',$skuAuth);
            $data['activate_exception_count_all']=$activateExceptionAllModel->count();

        //获取激活失败数 今日数
        $activateFailedModel = $ActivateUserManagementModel
            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_FAIL)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $activateFailedModel->whereIn('platform_sku',$skuAuth);
            $data['activate_failed_count_now']=$activateFailedModel->count();

        //获取激活失败数 总数
        $activateFailedAllModel = $ActivateUserManagementModel
            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_FAIL)
            ->where('mark','=',1);
        if (!empty($skuAuth)) $activateFailedModel->whereIn('platform_sku',$skuAuth);
            $data['activate_failed_count_all']=$activateFailedAllModel->count();

        //获取引导数 今日数
        $mainTable = $ActivateUserManagementModel->getTable();
        $infoTable = $ActivateUserManagementInfoModel->getTable();
        $guideCountModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData))
            ->where('m.mark','=',1)
            ->where('s.gift_type_2','>',0);
        if (!empty($skuAuth)) $guideCountModel->whereIn('m.platform_sku',$skuAuth);
        $data['guide_count_now']=$guideCountModel->count();

        //获取引导数 总数
        $guideCountAllModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('m.mark','=',1)
            ->where('s.gift_type_2','>',0);
        if (!empty($skuAuth)) $guideCountModel->whereIn('m.platform_sku',$skuAuth);
            $data['guide_count_all']=$guideCountAllModel->count();

        //获取留好评数 今日数
        $activateFavorableModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('m.mark','=',1)
            ->where('m.progress_favorable_comment','=',1)
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData))
            ->where('s.comment','!=','');
        if (!empty($skuAuth)) $activateFavorableModel->whereIn('m.platform_sku',$skuAuth);
        $data['activate_favorable_comment_count_now']=$activateFavorableModel->count();

        //获取留好评数 总数
        $activateFavorableAllModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('m.mark','=',1)
            ->where('m.progress_favorable_comment','=',1)
            ->where('s.comment','!=','');
        if (!empty($skuAuth)) $activateFavorableAllModel->whereIn('m.platform_sku',$skuAuth);
            $data['activate_favorable_comment_count_all']=$activateFavorableAllModel->count();

        //获取中差评数 今日数
        $activateBadModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('m.mark','=',1)
            ->where('m.evaluate_star_rating','<=',3)
            ->where('m.evaluate_star_rating','>=',0)
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData))
            ->where('s.comment','!=','');
        if (!empty($skuAuth)) $activateBadModel->whereIn('m.platform_sku',$skuAuth);
            $data['activate_bad_reviews_count_now']=$activateBadModel->count();

        //获取中差评数 总数
        $activateBadAllModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('m.mark','=',1)
            ->where('m.evaluate_star_rating','<=',3)
            ->where('m.evaluate_star_rating','>=',0)
            ->where('s.comment','!=','');
        if (!empty($skuAuth)) $activateBadAllModel->whereIn('m.platform_sku',$skuAuth);
            $data['activate_bad_reviews_count_all']=$activateBadAllModel->count();

        //获取完成流程数
        //配件 用户填完地址 今日数
        $accessoryNowModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_write_address','=',1)
            ->where('m.mark','=',1)
            ->where('s.gift_type_2','=',2)
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $accessoryNowModel->whereIn('m.platform_sku',$skuAuth);
            $accessoryNow=$accessoryNowModel->count();

        //配件 用户填完地址 总数
        $accessoryAllModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_write_address','=',1)
            ->where('m.mark','=',1)
            ->where('s.gift_type_2','=',2);
        if (!empty($skuAuth)) $accessoryAllModel->whereIn('m.platform_sku',$skuAuth);
            $accessoryAll=$accessoryAllModel->count();

        //非配件 留好评 今日数
        $accessoryFavorableNowModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_favorable_comment','=',1)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3])
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $accessoryFavorableNowModel->whereIn('m.platform_sku',$skuAuth);
            $accessoryFavorableNow=$accessoryFavorableNowModel->count();

        //非配件 留好评 总数
        $accessoryFavorableAllModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_favorable_comment','=',1)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3]);
        if (!empty($skuAuth)) $accessoryFavorableAllModel->whereIn('m.platform_sku',$skuAuth);
            $accessoryFavorableAll=$accessoryFavorableAllModel->count();

        //非配件 留差评 今日数
        $accessoryBadReviewsNowModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_bad_reviews','=',1)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3])
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $accessoryBadReviewsNowModel->whereIn('m.platform_sku',$skuAuth);
            $accessoryBadReviewsNow=$accessoryBadReviewsNowModel->count();

        //非配件 留差评 总数
        $accessoryBadReviewsAllModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_bad_reviews','=',1)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3]);
        if (!empty($skuAuth)) $accessoryBadReviewsAllModel->whereIn('m.platform_sku',$skuAuth);
            $accessoryBadReviewsAll=$accessoryBadReviewsAllModel->count();

        $data['flow_path_complete_count_now'] = $accessoryNow + $accessoryFavorableNow + $accessoryBadReviewsNow;
        $data['flow_path_complete_count_all'] = $accessoryAll + $accessoryFavorableAll + $accessoryBadReviewsAll;

        //获取订单数
        $OrderAndCustomersMainTable = $OrderAndCustomersOrderModel->getTable();
        $OrderAndCustomersSonTable = $OrderAndCustomersOrderDetailModel->getTable();

        $OrderAndCustomersModel = $OrderAndCustomersOrderModel::from("$OrderAndCustomersMainTable as m")
            ->select('m.id')
            ->join("$OrderAndCustomersSonTable as s", 's.order_id', '=', 'm.id')
            ->distinct('m.id')
            ->where('m.mark','=',1)
            ->where('m.create_time', '>=', strtotime($startDate))
            ->where('m.create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $OrderAndCustomersModel->whereIn('s.platform_sku',$skuAuth);
        $data['order_count_now'] = $OrderAndCustomersModel->count();

        $OrderAndCustomersAllModel = $OrderAndCustomersOrderModel::from("$OrderAndCustomersMainTable as m")
            ->select('m.id')
            ->join("$OrderAndCustomersSonTable as s", 's.order_id', '=', 'm.id')
            ->distinct('m.id')
            ->where('m.mark','=',1);
        if (!empty($skuAuth)) $OrderAndCustomersAllModel->whereIn('s.platform_sku',$skuAuth);
        $data['order_count_all'] = $OrderAndCustomersAllModel->count();

        //获取发送成功邮件数 今日数
        $emailModel = $SendLogModel
            ->where('mark','=',1)
            ->where('status','=',SendLogService::STATUS_SEND_SUCCESS)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $emailModel->whereIn('sku',$skuAuth);
        $data['email_send_success_count_now']=$emailModel->count();

        //获取发送成功邮件数 总数
        $emailAllModel = $SendLogModel
            ->where('status','=',SendLogService::STATUS_SEND_SUCCESS)
            ->where('mark','=',1);
        if (!empty($skuAuth)) $emailAllModel->whereIn('sku',$skuAuth);
            $data['email_send_success_count_all']=$emailAllModel->count();

        //获取发送失败邮件数 今日数
        $emailModel = $SendLogModel
            ->where('mark','=',1)
            ->where('status','=',SendLogService::STATSU_SEND_FAIL)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $emailModel->whereIn('sku',$skuAuth);
        $data['email_send_failed_count_now']=$emailModel->count();

        //获取发送失败邮件数 总数
        $emailAllModel = $SendLogModel
            ->where('status','=',SendLogService::STATSU_SEND_FAIL)
            ->where('mark','=',1);
        if (!empty($skuAuth)) $emailAllModel->whereIn('sku',$skuAuth);
        $data['email_send_failed_count_all']=$emailAllModel->count();

        //今日注册数 今日数
        $registerNumModel = $ActivateUserManagementModel
            ->whereIn('progress_activate',[RegisterService::PROGRESS_ACTIVATE_ALREADY,RegisterService::PROGRESS_ACTIVATE_ABNORMAL])
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($startDate))
            ->where('create_time', '<=', strtotime($endData));
        if (!empty($skuAuth)) $registerNumModel->whereIn('platform_sku',$skuAuth);
            $data['register_num_now']=$registerNumModel->count();
        //所有注册数
        $registerNumAll = $ActivateUserManagementModel
            ->whereIn('progress_activate',[RegisterService::PROGRESS_ACTIVATE_ALREADY,RegisterService::PROGRESS_ACTIVATE_ABNORMAL])
            ->where('mark','=',1);
        if (!empty($skuAuth)) $registerNumAll->whereIn('platform_sku',$skuAuth);
            $data['register_num_all']=$registerNumAll->count();


        //激活率 昨天平台的 注册数/扫码数
        $nowDate = date('Y-m-d',time()-24*60*60);
        $yesStartDate = $nowDate.' 00:00:00';
        $yesEndData = $nowDate.' 23:59:59';
        //昨天 平台激活成功的 （激活成功、激活异常）
        $activateSuccessYes = $ActivateUserManagementModel
            ->whereIn('progress_activate',[RegisterService::PROGRESS_ACTIVATE_ALREADY,RegisterService::PROGRESS_ACTIVATE_ABNORMAL])
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();
        //昨天 平台激活过的
        $activateToday = $ActivateUserManagementModel
//            ->where('progress_activate','=',RegisterService::PROGRESS_ACTIVATE_ALREADY)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();

        if ($activateToday != 0 || $activateSuccessYes !=0){
            $data['activate_rate_yesterday'] = number_format(($activateSuccessYes/$activateToday) * 100,2).'%';
        }else{
            $data['activate_rate_yesterday'] = '0.0%';
        }

        //引导率 昨天我的 选择礼品数/注册数
        //我的 选择礼品数
        $activateConfirmGiftSelfModel = $ActivateUserManagementModel
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('progress_confirm_gift','=',1)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $activateConfirmGiftSelfModel->whereIn('platform_sku',$skuAuth);
        $activateConfirmGiftSelf=$activateConfirmGiftSelfModel->count();
        //我的注册数
        $registerNumSelfModel = $ActivateUserManagementModel
            ->whereIn('progress_activate',[RegisterService::PROGRESS_ACTIVATE_ALREADY,RegisterService::PROGRESS_ACTIVATE_ABNORMAL])
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $registerNumSelfModel->whereIn('platform_sku',$skuAuth);
        $registerNumSelf=$registerNumSelfModel->count();

        if ($activateConfirmGiftSelf != 0 || $registerNumSelf !=0){
            $data['guide_rate_self_yesterday'] = number_format(($activateConfirmGiftSelf/$registerNumSelf) * 100,2).'%';
        }else{
            $data['guide_rate_self_yesterday'] = '0.0%';
        }

        //引导率 昨天平台的 选择礼品数/注册数
        //选择礼品数
        $activateConfirmGift = $ActivateUserManagementModel
            ->where('progress_confirm_gift','=',1)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();
        //注册数
        $registerNum = $ActivateUserManagementModel
            ->whereIn('progress_activate',[RegisterService::PROGRESS_ACTIVATE_ALREADY,RegisterService::PROGRESS_ACTIVATE_ABNORMAL])
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();

        if ($activateConfirmGift != 0 || $registerNum !=0){
            $data['guide_rate_yesterday'] = number_format(($activateConfirmGift/$registerNum) * 100,2).'%';
        }else{
            $data['guide_rate_yesterday'] = '0.0%';
        }

        //昨天我的 系统留好评率  系统留好评数/注册数
        //昨天我的 系统留好评数
        $activateFavorableCommentSelfModel = $ActivateUserManagementModel
            ->where('progress_favorable_comment','=',1)
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $activateFavorableCommentSelfModel->whereIn('platform_sku',$skuAuth);
        $activateFavorableCommentSelf=$activateFavorableCommentSelfModel->count();

        if ($activateFavorableCommentSelf != 0 || $registerNumSelf !=0){
            $data['favorable_comment_self'] = number_format(($activateFavorableCommentSelf/$registerNumSelf) * 100,2).'%';
        }else{
            $data['favorable_comment_self'] = '0.0%';
        }

        //昨天 系统留好评率  系统留好评数/注册数
        //昨天 系统留好评数
        $activateFavorableComment = $ActivateUserManagementModel
            ->where('progress_favorable_comment','=',1)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();
        if ($activateFavorableComment != 0 || $registerNum !=0){
            $data['favorable_comment'] = number_format(($activateFavorableCommentSelf/$registerNum) * 100,2).'%';
        }else{
            $data['favorable_comment'] = '0.0%';
        }

        //昨天我的 系统留中差评率  系统留中差评数/注册数
        //昨天我的 系统留中差评数
        $activateBadReviewsSelfModel = $ActivateUserManagementModel
            ->where('progress_bad_reviews','=',1)
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $activateBadReviewsSelfModel->whereIn('platform_sku',$skuAuth);
        $activateBadReviewsSelf=$activateBadReviewsSelfModel->count();
        if ($activateBadReviewsSelf != 0 || $registerNumSelf !=0){
            $data['bad_reviews_comment_self'] = number_format(($activateBadReviewsSelf/$registerNumSelf) * 100,2).'%';
        }else{
            $data['bad_reviews_comment_self'] = '0.0%';
        }

        //昨天平台的 系统留中差评率  系统留中差评数/注册数
        //昨天平台的 系统留中差评数
        $activateBadReviews = $ActivateUserManagementModel
            ->where('progress_bad_reviews','=',1)
            ->where('mark','=',1)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();
        if ($activateBadReviews != 0 || $registerNum !=0){
            $data['bad_reviews_comment'] = number_format(($activateBadReviews/$registerNum) * 100,2).'%';
        }else{
            $data['bad_reviews_comment'] = '0.0%';
        }

        //昨天我的 完成流程率  完成流程数/注册数
        //昨天我的 完成流程数
        //配件 用户填完地址
        $accessoryYesterdaySelfModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_write_address','=',1)
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('m.mark','=',1)
            ->where('s.gift_type_2','=',2)
            ->where('m.create_time', '>=', strtotime($yesStartDate))
            ->where('m.create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $accessoryYesterdaySelfModel->whereIn('platform_sku',$skuAuth);
        $accessoryYesterdaySelf=$accessoryYesterdaySelfModel->count();
        //非配件 留好评
        $accessoryFavorableYesterdaySelfModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_favorable_comment','=',1)
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3])
            ->where('m.create_time', '>=', strtotime($yesStartDate))
            ->where('m.create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $accessoryFavorableYesterdaySelfModel->whereIn('platform_sku',$skuAuth);
        $accessoryFavorableYesterdaySelf=$accessoryFavorableYesterdaySelfModel->count();
        //非配件 留差评
        $accessoryBadReviewsYesterdaySelfModel = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_bad_reviews','=',1)
            ->where('create_user','=',$VisitorsRecordModel->userId)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3])
            ->where('m.create_time', '>=', strtotime($yesStartDate))
            ->where('m.create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $accessoryBadReviewsYesterdaySelfModel->whereIn('platform_sku',$skuAuth);
        $accessoryBadReviewsYesterdaySelf=$accessoryBadReviewsYesterdaySelfModel->count();

        $successProgressNumSelf = $accessoryYesterdaySelf + $accessoryFavorableYesterdaySelf + $accessoryBadReviewsYesterdaySelf;

        if ($successProgressNumSelf != 0 || $registerNumSelf !=0){
            $data['success_progress_self'] = number_format(($successProgressNumSelf/$registerNumSelf) * 100,2).'%';
        }else{
            $data['success_progress_self'] = '0.0%';
        }


        //昨日我的 邮件打开率 打开过的/发送成功
        //发送成功
        $emailYesterdaySelfModel = $SendLogModel
            ->where('mark','=',1)
            ->whereIn('status',[SendLogService::STATUS_SEND_SUCCESS,SendLogService::STATUS_OPEN])
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $emailYesterdaySelfModel->whereIn('sku',$skuAuth);
        $emailSendSuccessSelfYesterday=$emailYesterdaySelfModel->count();

        //发送成功 并打开
        $emailOpenYesterdaySelfModel = $SendLogModel
            ->where('mark','=',1)
            ->where('status','=',SendLogService::STATUS_OPEN)
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->where('end_open_time', '>=', strtotime($yesStartDate))
            ->where('end_open_time', '<=', strtotime($yesEndData));
        if (!empty($skuAuth)) $emailOpenYesterdaySelfModel->whereIn('sku',$skuAuth);
        $emailOpenSelfYesterday=$emailOpenYesterdaySelfModel->count();

        if ($emailSendSuccessSelfYesterday != 0 || $emailOpenSelfYesterday !=0){
            $data['email_open_rate_self_yesterday'] = number_format(($emailOpenSelfYesterday/$emailSendSuccessSelfYesterday) * 100,2).'%';
        }else{
            $data['email_open_rate_self_yesterday'] = '0.0%';
        }

        //昨日平台 邮件打开率 打开过的/发送成功
        //发送成功
        $emailSendSuccessYesterday = $SendLogModel
            ->where('mark','=',1)
            ->whereIn('status',[SendLogService::STATUS_SEND_SUCCESS,SendLogService::STATUS_OPEN])
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();

        //发送成功 并打开
        $emailOpenYesterday = $SendLogModel
            ->where('mark','=',1)
            ->where('status','=',SendLogService::STATUS_OPEN)
            ->where('end_open_time', '>=', strtotime($yesStartDate))
            ->where('end_open_time', '<=', strtotime($yesEndData))
            ->where('create_time', '>=', strtotime($yesStartDate))
            ->where('create_time', '<=', strtotime($yesEndData))
            ->count();

        if ($emailSendSuccessYesterday != 0 || $emailOpenYesterday !=0){
            $data['email_open_rate_yesterday'] = number_format(($emailOpenYesterday/$emailSendSuccessYesterday) * 100,2).'%';
        }else{
            $data['email_open_rate_yesterday'] = '0.0%';
        }

        //昨天平台的 完成流程数
        //配件 用户填完地址
        $accessoryNowYesterday = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_write_address','=',1)
            ->where('m.mark','=',1)
            ->where('s.gift_type_2','=',2)
            ->where('m.create_time', '>=', strtotime($yesStartDate))
            ->where('m.create_time', '<=', strtotime($yesEndData))
            ->count();
        //非配件 留好评
        $accessoryFavorableYesterday = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_favorable_comment','=',1)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3])
            ->where('m.create_time', '>=', strtotime($yesStartDate))
            ->where('m.create_time', '<=', strtotime($yesEndData))
            ->count();
        //非配件 留差评
        $accessoryBadReviewsYesterday = $ActivateUserManagementModel::from("$mainTable as m")
            ->join("$infoTable as s", 's.activate_user_id', '=', 'm.id')
            ->where('m.progress_bad_reviews','=',1)
            ->where('m.mark','=',1)
            ->whereIn('s.gift_type_2',[1,3])
            ->where('m.create_time', '>=', strtotime($yesStartDate))
            ->where('m.create_time', '<=', strtotime($yesEndData))
            ->count();

        $successProgressNumYester = $accessoryNowYesterday + $accessoryFavorableYesterday + $accessoryBadReviewsYesterday;

        if ($successProgressNumYester != 0 || $registerNum !=0){
            $data['success_progress_yesterday'] = number_format(($successProgressNumYester/$registerNum) * 100,2).'%';
        }else{
            $data['success_progress_yesterday'] = '0.0%';
        }

        //获取最近30天的表格图数据
        $data = self::getEchartsData($data);
        return $data;
    }


    /**
     * @Notes:获取表格 访客数、激活客户、留评数量
     * @Interface getEchartsData
     * @return array
     * @author: wuyu
     * @Time: 2023/6/9   16:42
     */
    public static function getEchartsData($data)
    {
        $days = array (); #首先定义一个空数组
        //权限过滤 组员可以看一个组的信息
        $skuAuth = CommonService::skuAuth();
        for($i=0;$i<=30;$i++)
        {
            $days[] = date("Y-m-d", strtotime('-'.$i.'day'));
        }
        $days = array_reverse($days);
        $VisitorsRecordModel = new VisitorsRecordModel();
        $ActivateUserManagementModel = new ActivateUserManagementModel();

        foreach ($days as $val){
            $startDate = $val.' 00:00:00';
            $endData = $val.' 23:59:59';
            $data['date_time'][] = substr($val,5);

            //获取访客数
            $data['visitors_data'][] = $VisitorsRecordModel
                ->where('mark','=',1)
                ->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<=', strtotime($endData))
                ->count();

            //获取激活客户
            $activateUserModel = $ActivateUserManagementModel
                ->whereIn('progress_activate',[RegisterService::PROGRESS_ACTIVATE_ALREADY,RegisterService::PROGRESS_ACTIVATE_ABNORMAL])
                ->where('mark','=',1)
                ->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<=', strtotime($endData));
            if (!empty($skuAuth)) $activateUserModel->whereIn('platform_sku',$skuAuth);
            $data['activate_user'][]=$activateUserModel->count();

            //获取留评数量
            //留中差评数量
            $badReviewModel = $ActivateUserManagementModel
                ->where('mark','=',1)
                ->where('progress_bad_reviews','=',1)
                ->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<=', strtotime($endData));
            if (!empty($skuAuth)) $badReviewModel->whereIn('platform_sku',$skuAuth);
            $badReviewCount=$badReviewModel->count();
            //留好评数
            $favorableCommentModel = $ActivateUserManagementModel
                ->where('mark','=',1)
                ->where('progress_favorable_comment','=',1)
                ->where('create_time', '>=', strtotime($startDate))
                ->where('create_time', '<=', strtotime($endData));
            if (!empty($skuAuth)) $favorableCommentModel->whereIn('platform_sku',$skuAuth);
            $favorableComment=$favorableCommentModel->count();

            $data['comment_count'][] = $badReviewCount + $favorableComment;
        }
        return $data;
    }

}
